﻿using System;
using System.Web;
using System.Web.Mvc;

public class PageBean 
{
    private string url = "";
    /// <summary>
    /// 分页实体lei
    /// </summary>
    /// <param name="_index"></param>
    /// <param name="_size"></param>
    /// <param name="_count"></param>
    public PageBean(int? _index,int _size,int _count,Controller thiscl)
    {
        Index = _index==null?1:_index.Value;
        Size = _size;
        PageCount = GetCount(_count, _size);
        Index=(Index < 1 || Index > PageCount) ? 1 : Index;
        var ac=  thiscl.RouteData.Values["controller"];
        url = thiscl.Url.Content("~/"+ac);
        Id = thiscl.RouteData.Values["id"];
    }

    /// <summary>
    /// 计算总页数
    /// </summary>
    /// <param name="_count">总项数</param>
    /// <param name="_size">每页大小</param>
    /// <returns></returns>
    private int GetCount(int _count,int _size)
    {
        if (_count == 0) return 0;
        if (_count < _size)
        {
            return 1;
        }
        if (_count % _size == 0)
        {
            return (_count / _size);
        }
        else {
            return (_count / _size)+1;
        }
    }

    /// <summary>
    /// 起始页
    /// </summary>
    public int Index { get; set; }

    /// <summary>
    /// 每页大小
    /// </summary>
    private int Size { get; set; }

    private object Id { get; set; }

    /// <summary>
    /// 总页数
    /// </summary>
    public int PageCount { get; set; }

    public string GetUrlFormat(int _index)
    { 
        string param =System.Web.HttpContext.Current.Request.Url.Query;
        return string.Format("{0}/{1}/p{2}{3}", url,Id, _index, param);
    }
}